package com.example.edu.assertmatcher.matcher;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;

/**
 * @author yanzhao
 * @date 2022/2/10 14:43
 * @since 1.0.0
 */
public class CompareIntegerMatcher extends BaseMatcher<Integer> {

    private final Integer value;

    private final boolean great;

    public CompareIntegerMatcher(Integer value, boolean great) {
        this.value = value;
        this.great = great;
    }

    @Override
    public boolean matches(Object o) {

        final Class<?> clazz = o.getClass();
        if (clazz == Integer.class) {
            return great ? (Integer) o > (Integer) value : (Integer) o < (Integer) value;
        } else {
            throw new AssertionError("The number type " + clazz + " not supported.");
        }
    }

    public static CompareIntegerMatcher gt(Integer value) {
        return new CompareIntegerMatcher(value, true);
    }

    public static CompareIntegerMatcher lt(Integer value) {
        return new CompareIntegerMatcher(value, false);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("compare two number failed.");
    }

}
